package com.yz.conroller; /**
 * @className: ${NAME}
 * @author: shuishui
 * @date: 2022/12/03 17:16
 */
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yz.core.utils.R;
import com.yz.entity.SystemUserEntity;
import com.yz.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/SystemUserController")
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

//    @GetMapping("/list")
//    @ResponseBody
//    public R list(ModelMap modelMap) throws Exception {
//        Page page =new Page();
//        page.setSize(1);
//
//        // 1.查询数据
//        systemUserService.myList(page);
//        List<SystemUserEntity> list =page.getRecords();
//                // 2.把数据放到Map中
//        modelMap.put("sysUserList",list);
//        // 3.跳转到视图
//        return R.ok().put("data",list);
//    }

    @PostMapping("/add")
    @ResponseBody
    public R add(@RequestBody SystemUserEntity systemUserEntity) throws Exception {
       systemUserEntity.setStatus(0);
       boolean save = systemUserService.save(systemUserEntity);
       return R.out(save);
    }
    @GetMapping("/SystemUserById/{id}")
    @ResponseBody
    public R systemUserById(@PathVariable Integer id,ModelMap map) throws Exception {

//        String id = this.req.getParameter("id");
        SystemUserEntity systemUser = systemUserService.getById(id);
        return R.ok().put("data",systemUser);
//        Map<String, Object> map = new HashMap<>();
//        map.put("systemUser", systemUser);
//        outView("/system/sysUser/sysUserUpdate", map);
    }
    @PostMapping("/update")
    @ResponseBody
    public R update(@RequestBody SystemUserEntity systemUserEntity) throws Exception {
        boolean save = systemUserService.updateById(systemUserEntity);
        return R.out(save);
//        String json = super.req.getReader().readLine();
//        SystemUserEntity systemUser = JSON.parseObject(json, SystemUserEntity.class);
//        boolean save = systemUserService.update(systemUser);
//        return outJson(save);
    }
    @DeleteMapping("delete/{id}")
    @ResponseBody
    public R delete(@PathVariable Integer id) throws Exception {
        boolean remove = systemUserService.removeById(id);
        return R.ok();
//        String id = super.req.getParameter("id");
//        boolean remove = systemUserService.remove(Integer.parseInt(id));
//        return BaseConstants.BASE_REDIRECT + ":" + "SystemUserController/list";
    }
    @DeleteMapping("batchDel")
    @ResponseBody
    public R batchDel(@RequestBody List<Integer> list) throws Exception {
        return R.out(systemUserService.removeBatchByIds(list));
//        String json = req.getReader().readLine();
//        List<Integer> list = JSON.parseArray(json, Integer.class);
//        return outJson(systemUserService.batchRemove(list));
    }
    @PostMapping("/page")
    @ResponseBody
    public R page(@RequestBody Map<String,Object> param) { // JSON
        // 1.调用业务层给page中的色属性赋值
        Object current = param.get("current");
        Object size = param.get("size");
        Object username = param.get("username");
        Page<SystemUserEntity> page = new Page<>();
        if(!ObjectUtils.isEmpty(current)){
            page.setCurrent(Integer.parseInt(current.toString()));
        }
        if(!ObjectUtils.isEmpty(size)){
            page.setSize(Integer.parseInt(size.toString()));
        }

        QueryWrapper wrapper = new QueryWrapper();
        if(!ObjectUtils.isEmpty(username)){
            wrapper.like("username",username);
        }
        systemUserService.page(page,wrapper);
        page.setTotal(systemUserService.count());
        // 2.响应数据
        return R.ok().put("data", page);
    }


}
